/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Customer;

import java.util.List;
import javax.swing.JOptionPane;
import supermarket.*;
import model.*;

/**
 *
 * @author aggelos
 */
public class PayForm extends javax.swing.JFrame {

    private final MainForm parent;
    private final MarketPlacePanel MP;
    private CreditCard cc;
    private List<model.Bank> Banks;
    private final CreditCardVerifier ccVerifier;

    /**
     * Creates new form PayForm
     *
     * @param parent
     * @param MP
     */
    public PayForm(MainForm parent, MarketPlacePanel MP) {
        this.parent = parent;
        this.MP = MP;
        this.cc = this.parent.GetUser().getCreditCard();
        this.ccVerifier = new CreditCardVerifier(this.parent.db);
        this.setLocationRelativeTo(null);
        initComponents();
        this.populateBanksCombo();
        this.showCreditCardInfo(this.cc);
    }

    private void showCreditCardInfo(CreditCard local_cc) {
        //Αν η κάρτα υπάρχει δείξε την αλλιώς φτιάξε μια κενή καινούργια κάρτα μιας χρήσης
        if (local_cc != null) {
            this.number.setText(local_cc.getNumber());
            this.cvv.setText(String.valueOf(local_cc.getCvv()));
            this.owner.setText(local_cc.getOwnerName());
            // Δείξε την τράπεζα του πελάτη στο κόμπο
            this.BanksCombo.setSelectedIndex(this.cc.getBank().getBankId() - 1);
        } else {
            this.cc = new CreditCard();
        }
    }

    private void populateBanksCombo() {
        this.Banks = parent.db.getAllBanks();
        //Βάζουμε τα στοιχεία στο Combo 
        for (int i = 0; i < Banks.size(); i++) {
            this.BanksCombo.addItem(Banks.get(i).getName());
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        BanksCombo = new javax.swing.JComboBox();
        owner = new javax.swing.JTextField();
        number = new javax.swing.JTextField();
        cvv = new javax.swing.JTextField();
        b_pay = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setAlwaysOnTop(true);
        setResizable(false);
        setType(java.awt.Window.Type.UTILITY);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosed(java.awt.event.WindowEvent evt) {
                formWindowClosed(evt);
            }
        });

        jPanel1.setBackground(new java.awt.Color(248, 127, 7));
        jPanel1.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
        jPanel1.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));

        jLabel1.setFont(new java.awt.Font("Ubuntu", 1, 24)); // NOI18N
        jLabel1.setText("Πληρωμή");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jLabel1)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addGap(0, 0, Short.MAX_VALUE)
                .addComponent(jLabel1))
        );

        BanksCombo.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                BanksComboMouseClicked(evt);
            }
        });

        b_pay.setText("Πληρωμή");
        b_pay.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                b_payMouseClicked(evt);
            }
        });

        jLabel2.setText("Τράπεζα :");

        jLabel3.setText("Κάτοχος :");

        jLabel4.setText("Αριθμός :");

        jLabel5.setText("CVV :");

        jLabel6.setText("Στοιχεία Πιστωτικής");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel6, javax.swing.GroupLayout.DEFAULT_SIZE, 222, Short.MAX_VALUE)
                        .addGap(42, 42, 42))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel3)
                            .addComponent(jLabel4)
                            .addComponent(jLabel5))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(BanksCombo, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(owner)
                            .addComponent(number)
                            .addComponent(b_pay, javax.swing.GroupLayout.DEFAULT_SIZE, 202, Short.MAX_VALUE)
                            .addComponent(cvv, javax.swing.GroupLayout.PREFERRED_SIZE, 66, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel6)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(BanksCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel2))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(owner, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel3))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(number, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel4))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(cvv, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jLabel5, javax.swing.GroupLayout.Alignment.TRAILING))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(b_pay)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void b_payMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_b_payMouseClicked

        //Η τράπεζα είτε την άλαξε ο χρήστης είτe ήρθε απο τη βάση είναι ΟΚ
        // Παίρνουμε τα υπόλοιπα στοιχεία απο το gui
        this.cc.setOwnerName(this.owner.getText());
        this.cc.setNumber(this.number.getText());
        this.cc.setCvv(Integer.parseInt(this.cvv.getText()));

        // Ελεγξε τα στοιχεία της κάρτας
        if (this.ccVerifier.verifyCard(cc)) {
            // Επιτυχία
            JOptionPane.showMessageDialog(this, "Επιτυχημένη Συναλαγή", "Επιτυχημένη Συναλαγή", JOptionPane.INFORMATION_MESSAGE);
            this.MP.succesfulTransaction();
        } else {
            // Αποτυχία
            int retries = this.ccVerifier.getNumOfTries();
            if (retries < 3) {
                String msg = "Τα στοιχεία της πιστωτικής κάρτας δεν είναι έγκυρα!";
                msg += "\nΑπομένουν " + (3 - retries) + " προσπάθειες.";
                JOptionPane.showMessageDialog(this, msg, "Σφάλμα", JOptionPane.WARNING_MESSAGE);
                return;
            } else {
                this.MP.failedTransaction();
            }
        }
        //Κλείσε την αυτή την φόρμα και ενεργοποίησε την κεντρική φόρμα     
        parent.setEnabled(true);
        this.dispose();    }//GEN-LAST:event_b_payMouseClicked


    private void formWindowClosed(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosed
        //Κλείσε την αυτή την φόρμα και ενεργοποίησε την κεντρική φόρμα     
        parent.setEnabled(true);
    }//GEN-LAST:event_formWindowClosed

    private void BanksComboMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_BanksComboMouseClicked
             if (this.BanksCombo.getSelectedIndex() > -1) {

            // Αλαξε την τράπεζα στην κάρτα
            Bank B = this.Banks.get(this.BanksCombo.getSelectedIndex());
            this.cc.setBank(B);
        }
    }//GEN-LAST:event_BanksComboMouseClicked

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(PayForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(PayForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(PayForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(PayForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        //  java.awt.EventQueue.invokeLater(new Runnable() {
        //  public void run() {
        //     new PayForm().setVisible(true);
        //  }
        // });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox BanksCombo;
    private javax.swing.JButton b_pay;
    private javax.swing.JTextField cvv;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTextField number;
    private javax.swing.JTextField owner;
    // End of variables declaration//GEN-END:variables
}
